//
//  MoreSortVC.m
//  reSearchDemo
//
//  Created by 云库 on 15/9/23.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "MoreSortVC.h"
#import "MoreSortVC.h"
#import "AFNetworking.h"
#import "MJExtension.h"
#import "MBProgressHUD.h"
#import "GoodsClassInfo.h"
#import "GoodsClass.h"
#import "BrandList.h"
#import "UIView+Category.h"
#import "MJRefresh.h"
#import "AllSubSortVC.h"
#import "MoreSortCell.h"

@interface MoreSortVC ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) NSMutableArray *sortArray;
@property (strong, nonatomic) UITableView *tableView;

@property (strong, nonatomic) MBProgressHUD *hud;
@property (nonatomic,assign) int pageno;

//存储路径
@property (strong, nonatomic)NSString *documentsPath;

@end

@implementation MoreSortVC


- (NSString *)documentsPath
{
    if (!_documentsPath) {
        _documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    }
    return _documentsPath;
}

- (NSArray *)sortArray{
    if (!_sortArray) {
        _sortArray = [NSMutableArray array];
    }
    return _sortArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
        self.title = @"团购分类";
    /** 加载数据*/
    [self loadDataFromServer];
    [self createTableView];
    
    /** 刷新*/
    [self setupDownRefresh];
    [self setupUpRefresh];
    

}
/** 下拉刷新*/
- (void)setupDownRefresh
{
    // 1.添加刷新控件
    [self.tableView addHeaderWithTarget:self action:@selector(loadNewData)];
    
    // 2.进入刷新状态
    [self.tableView headerBeginRefreshing];
    
}

/** 上拉刷新*/
- (void)setupUpRefresh
{
    [self.tableView addFooterWithTarget:self action:@selector(loadMoreData)];
}


//加载新的订单
- (void)loadNewData{
    self.pageno = 1;
    [self loadDataFromServer];
}

- (void)loadMoreData{
    
    //设置其他参数
    self.pageno++;
    //重新发送请求
    [self loadDataFromServer];
}


- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.self.tableView animated:YES];
    self.hud.labelText = @"正在拼命加载中....";
    self.hud.dimBackground = NO;
}


- (void)loadDataFromServer{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"pageno"] = @(self.pageno);
    NSString *path = [NSString stringWithFormat:@"%@%@",SHOPER,@"html/agoods/goodsType.do"];
    [manager GET:path parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        DLog(@"responseObject = %@",responseObject);
        //存数据-------以下测试用
        //准备路径
//        NSString *filePath = [self.documentsPath stringByAppendingPathComponent:@"sort.plist"];
//        
//        
//        NSArray *sorttt = [GoodsClass objectArrayWithKeyValuesArray:responseObject[@"result"][@"DATA"]];
//
//            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//            dict[@"DATA"] = [GoodsClass keyValuesArrayWithObjectArray:sorttt];
//        
//            [dict writeToFile:filePath atomically:YES];
//        
//        
//        DLog(@"---000----路径--%@",filePath);
        
        
        //存数据-------以上测试用
        
        
        
        [self.hud hide:YES];
        
        if (self.pageno == 1) {
            //将之前旧的数据清空
            [self.sortArray removeAllObjects];
        }
        
        GoodsClassInfo *info = [GoodsClassInfo objectWithKeyValues:responseObject[@"result"]];
        NSArray *array = info.DATA;
        
        
        
        [self.sortArray addObjectsFromArray:array];
        [self.tableView reloadData];
        
        
        // 结束刷新刷新
        [self.tableView headerEndRefreshing];
        // 结束刷新刷新
        [self.tableView footerEndRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 结束刷新刷新
        [self.tableView footerEndRefreshing];
        DLog(@"请求网络失败%@",self.class);
    }];
    
}


#pragma mark - **************** TableViewDelegate的方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.sortArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MoreSortCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[MoreSortCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    GoodsClass *sort = self.sortArray[indexPath.row];
//    cell.textLabel.text = sort.name;
    cell.row = indexPath.row;
    cell.model = sort.name;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DLog(@"点击了cell");
    
    AllSubSortVC *subSort = [[AllSubSortVC alloc] init];
    GoodsClass *sort = self.sortArray[indexPath.row];
    subSort.brandArr = sort.brandList;
    [self pushViewController:subSort];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
